package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.Examine;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 审批操作表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-07-06
 */
@Mapper
public interface ExamineMapper extends BaseMapper<Examine> {


    /**
     * 通过关联id和流程类型获取当前审核流程的主要数据
     * @param id
     * @param type
     * @return
     */
    @Select("SELECT M.* FROM( " +
            "SELECT de.*,ddd.name dictsortName,da.username auditName,da3.username applicantName,d.name as departmentName,f.name as positionName FROM " +
            "    (select * from dragon_examine ) de " +
            "        left join dragon_dictsort_detail ddd on ddd.value=de.stream_type and code_type='streamCode' " +
            "        left join dragon_admin da on de.audit_id = da.id " +
            "        left join dragon_admin da3 on de.applicant_id = da3.id " +
            " left join dragon_admin_department c on da.id = c.admin_id "+
            " left join dragon_department d on c.department_id = d.id  "+
            " left join dragon_admin_position e on da.id = e.admin_id " +
            " left join dragon_position f on f.id = e.position_id" +
            " left join dragon_examine_user deu on deu.id = de.examine_id "+
            "where de.correlation_id = #{id} and de.stream_type = #{type} )M limit 1 ")
    Examine selectDetailsById(Integer id,Integer type);

    @Select(
            "SELECT  de.*,ddd.name dictsortName,da.username auditName,da3.username applicantName,d.name as departmentName,f.name as positionName, h.create_time as contractCreateTime FROM " +
            "     (select * from dragon_examine ) de " +
            "        left join dragon_dictsort_detail ddd on ddd.value=de.stream_type and code_type='streamCode' " +
            "        left join dragon_admin da on de.audit_id = da.id " +
            "        left join dragon_admin da3 on de.applicant_id = da3.id " +
            " left join dragon_admin_department c on da.id = c.admin_id "+
            " left join dragon_department d on c.department_id = d.id  "+
            " left join dragon_admin_position e on da.id = e.admin_id " +
            " left join dragon_position f on f.id = e.position_id " +
                    " left join dragon_contract h on h.id = de.correlation_id "+
            "${ew.customSqlSegment}  ")
    List<Examine> selectExamineList(@Param(Constants.WRAPPER) Wrapper<Examine> queryWrapper);

    /**
     * 获取申请分页
     * @param page
     * @param wrapper
     * @return
     */
    @Select("select M.* from (SELECT de.*, de.status as totalStatus , ddd.name dictsortName, da.username applicantName FROM dragon_examine de " +
            "LEFT JOIN dragon_dictsort_detail ddd ON ddd.value = de.stream_type AND code_type = 'streamCode' " +
            "LEFT JOIN dragon_admin da ON de.applicant_id = da.id)M " +
            "${ew.customSqlSegment}")
    Page<Examine> selectPageList(Page<Examine> page, @Param(Constants.WRAPPER)Wrapper<Examine> wrapper);

    /**
     * 获取审批分页列表
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT de.* ,deu.audit_user_id as examineAuditUserId, " +
            "deu.id as examineUserId, " +
            "deu.status as examineStatus, " +
            "deu.audit_time as examineAuditTime, " +
            "deu.pre_status as examinePreStatus, " +
            "da.username applicantName " +
            "FROM dragon_examine_user deu " +
            "LEFT JOIN dragon_examine de ON deu.examine_id = de.id " +
            "LEFT JOIN dragon_dictsort_detail ddd ON ddd.value = de.stream_type " +
            "AND code_type = 'streamCode' " +
            "LEFT JOIN dragon_admin da ON deu.audit_user_id = da.id " +
            "AND da.state = 1 AND da.deleted = 0 " +
            "${ew.customSqlSegment} ")
    Page<Examine> selectAuditPageList(Page<Examine> page, @Param(Constants.WRAPPER)Wrapper<Examine> wrapper);

    @Select(" select de.*,deu.audit_user_id examineAuditUserId,deu.id examineUserId,deu.status examineStatus," +
            "deu.pre_status examinePreStatus,deu.opinion,deu.audit_time examineAuditTime,da.username auditName," +
            "da1.username applicantName, ddd.name dictsortName from dragon_examine_user deu " +
            " left join dragon_examine de on deu.examine_id = de.id " +
            "left join dragon_dictsort_detail ddd on ddd.value=de.stream_type and code_type='streamCode' " +
            " left join dragon_admin da on deu.audit_user_id = da.id and da.state = 1 and da.deleted = 0 " +
            "left join dragon_admin da1 on de.applicant_id = da1.id and da1.state = 1 and da1.deleted = 0  " +
            " ${ew.customSqlSegment} ")
    List<Examine> selectExamineListByTypeAndMainId(@Param(Constants.WRAPPER)Wrapper<Examine> wrapper);

}
